package com.extweb4j.web.controller;

import com.extweb4j.core.anno.AuthAnno;
import com.extweb4j.core.anno.Log;
import com.extweb4j.core.bean.PageInfo;
import com.extweb4j.core.controller.ExtController;
import com.extweb4j.core.model.ExtNews;
import com.extweb4j.core.model.ExtTender;
import com.extweb4j.core.model.ExtUser;
import com.extweb4j.core.model.ExtUserRole;
import com.extweb4j.web.bean.News;
import com.extweb4j.web.bean.Supplier;
import com.extweb4j.web.utils.DateUtils;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description: .
 * @author: ZhaoWeiNan .
 * @createdTime: 2018/12/13 .
 * @version: 1.0 .
 */
public class NewsController extends ExtController {

    /**
     * 列表
     */
    @AuthAnno
    public void list(){
        int page = getParaToInt("page");
        int limit = getParaToInt("limit");
        String keywords = getPara("keywords");

//        PageInfo<News> pageInfo = new PageInfo<>();
        Page<ExtNews> pageData = ExtNews.dao.pageNewsBy(page,limit,keywords);

        renderJson(pageData);

//        News news1 = new News();
//        news1.setId(1L);
//        news1.setTitle("鼎信招标网郑重声明");
//        news1.setCreate_time(new Date());
//        news1.setContent("通辽市蒙东能源鼎信招投标有限责任公司（简称：鼎信招标网）...");
//        news1.setType("通知公告");
//        news1.setState(0);
//        news1.setUser("超级系统管理员");
//
//        News news2 = new News();
//        news2.setId(2L);
//        news2.setTitle("进博会上 国家电投签约185亿元");
//        news2.setCreate_time(new Date());
//        news2.setContent("进博会上 国家电投签约185亿元");
//        news2.setType("公司新闻");
//        news2.setState(0);
//        news2.setUser("超级系统管理员");
//
//        List<News> list = new ArrayList<>();
//        list.add(news1);
//        list.add(news2);
//
//        pageInfo.setFirstPage(true);
//        pageInfo.setLastPage(true);
//        pageInfo.setPageNumber(1);
//        pageInfo.setPageSize(20);
//        pageInfo.setTotalPage(1);
//        pageInfo.setTotalRow(2);

       // pageInfo.setList(list);
      //  renderJson(pageInfo);
    }


    /**
     * 编辑
     */
    @Log("编辑新闻")
    @AuthAnno
    @Before(Tx.class)
    public void edit(){
        ExtNews news = new ExtNews();
        news.set("id",getParaToInt("id"));
        news.set("title",getPara("title"));
        news.set("channelid",getParaToInt("ChannelId"));
        news.set("summary",getPara("summary"));
        news.set("source",getPara("source"));
        news.set("IsChecked",0);
        news.set("IsCircle",getParaToInt("IsCircle"));
        news.set("IsTop",getParaToInt("IsTop"));
        //news.set("create_time",new Timestamp(System.currentTimeMillis()));
       // news.set("creater",getPara("creater"));
        news.set("content",getPara("content"));
        news.update();
        success();
    }

    /**
     * 新增
     */
    @Log("新建招标")
    @AuthAnno
    @Before(Tx.class)
    public void add(){
        //ExtUser user = getExtModel(ExtUser.class);
        ExtNews news = new ExtNews();
        news.set("title",getPara("title"));
        news.set("source",getPara("source"));
        news.set("summary",getPara("summary"));
        news.set("IsCircle",getParaToInt("IsCircle"));
        news.set("IsTop",getParaToInt("IsTop"));
        news.set("ChannelId",getParaToInt("ChannelId"));
        news.set("content",getPara("content"));
        news.set("IsChecked",0);
        String userid=this.getRequest().getSession().getAttribute("uid").toString();
        ExtUser user =  ExtNews.dao.getUserNameByUserId(userid);
        news.set("Creater",user.getStr("user_name"));
        news.set("createtime",new Timestamp(System.currentTimeMillis()));
        news.save();
        success();
    }


    /**
     * 根据Id获取一条记录，编辑
     */
    public void get(){
        ExtNews tender = ExtNews.dao.findNewsById(getPara("id"));
        renderJson(new Record().set("success",true).set("data",tender));
    }


    /**
     * 逻辑删除
     */
    @Log("删除新闻")
    @AuthAnno
    @Before(Tx.class)
    public void delete(){
        ExtNews news = ExtNews.dao.findNewsById(getPara("id"));
        if(news == null){
            throw new RuntimeException("要删除的用户不存在");
        }
        news.delete();
        success();
    }
}
